﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.BLL.Extend;

namespace ZX.WebApp.EHR
{
    public partial class hrSaveValues : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetValues();
            }

            string flag = this.GetStringFromQueryString("flag", "");
            string AllowLateTimes = this.GetStringFromQueryString("AllowLateTimes", "");
            string AllowAdvancedTimes = this.GetStringFromQueryString("AllowAdvancedTimes", "");
            string AllowLateCounts = this.GetStringFromQueryString("AllowLateCounts", "");
            string AbsenceTimes = this.GetStringFromQueryString("AbsenceTimes", "");

            if (flag.Trim() == "save")
            {
                this.SaveData(AllowLateTimes, AllowAdvancedTimes, AllowLateCounts, AbsenceTimes);
            }
        }

        /// <summary>
        /// 取得考勤阀值初始值
        /// </summary>
        private void GetValues()
        {
            DatPhraseDetail instance = ZX.Common.Globals.getEntity("DatPhraseDetail") as DatPhraseDetail;
            BaseBussnies datBll = new BaseBussnies(instance);
            string condition = "ClassID=(select ClassID from DatPhraseClass where ClassValue='WorkTimeResultValves')";
            foreach (DatPhraseDetail item in datBll.GetEntitysByCondition(condition))
            {
                if (item.PhraseText.ToLower() == "AllowLateTimes".ToLower())
                {
                    AllowLateTimes.Value = item.PhraseValue;
                }
                else if (item.PhraseText.ToLower() == "AllowAdvancedTimes".ToLower())
                {
                    AllowAdvancedTimes.Value = item.PhraseValue;
                }
                else if (item.PhraseText.ToLower() == "AllowLateCounts".ToLower())
                {
                    AllowLateCounts.Value = item.PhraseValue;
                }
                else if (item.PhraseText.ToLower() == "AbsenceTimes".ToLower())
                {
                    AbsenceTimes.Value = item.PhraseValue;
                }
            }

        }

        /// <summary>
        /// 保存设置
        /// </summary>
        protected void SaveData(string allowLateTimes, string allowAdvancedTimes, string allowLateCounts, string absenceTimes)
        {
            try
            {
                DatPhraseDetail instance = ZX.Common.Globals.getEntity("DatPhraseDetail") as DatPhraseDetail;
                BaseBussnies datBll = new BaseBussnies(instance);
                string condition = "ClassID=(select ClassID from DatPhraseClass where ClassValue='WorkTimeResultValves')";
                foreach (DatPhraseDetail item in datBll.GetEntitysByCondition(condition))
                {
                    if (item.PhraseText.ToLower() == "AllowLateTimes".ToLower())
                    {
                        item.PhraseValue = allowLateTimes;
                    }
                    else if (item.PhraseText.ToLower() == "AllowAdvancedTimes".ToLower())
                    {
                        item.PhraseValue = allowAdvancedTimes;
                    }
                    else if (item.PhraseText.ToLower() == "AllowLateCounts".ToLower())
                    {
                        item.PhraseValue = allowLateCounts;
                    }
                    else if (item.PhraseText.ToLower() == "AbsenceTimes".ToLower())
                    {
                        item.PhraseValue = absenceTimes;
                    }
                    datBll.Update(item);
                }
                HttpContext.Current.Response.Write("1");
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("0");
                throw ex;
            }
        }
    }
}